//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.liy.framework.common.starter;

import com.liy.framework.common.orm.dialect.DialectFactory;
import com.liy.framework.common.orm.mybatis.interceptor.PaginationResultSetHandlerInterceptor;
import com.liy.framework.common.orm.mybatis.interceptor.PaginationStatementHandlerInterceptor;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

@Configuration
@EnableTransactionManagement
public class MyBatisAutoConfiguration implements TransactionManagementConfigurer {
    private static Logger log = LoggerFactory.getLogger(MyBatisAutoConfiguration.class);
    @Autowired
    private DataSource dataSource;

    public MyBatisAutoConfiguration() {
    }

    @Value("${mybatis.dialect-class-name: MySqlDialect}")
    public void initMybatisDialect(String dialectClass) {
        DialectFactory.dialectClass = dialectClass;
    }

    @Value("${mybatis.mapper-locations: classpath*:mapper/**/**.xml}")
    @Bean(
            name = {"sqlSessionFactory"}
    )
    public SqlSessionFactory sqlSessionFactoryBean(String mapperLocations) {
        log.info("<<<<<< MyBatisConfig init ... >>>>>>");
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(this.dataSource);
        PaginationResultSetHandlerInterceptor paginationResultSetHandlerInterceptor = new PaginationResultSetHandlerInterceptor();
        PaginationStatementHandlerInterceptor paginationStatementHandlerInterceptor = new PaginationStatementHandlerInterceptor();
        bean.setPlugins(new Interceptor[]{paginationResultSetHandlerInterceptor, paginationStatementHandlerInterceptor});
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            bean.setMapperLocations(resolver.getResources(mapperLocations));
            return bean.getObject();
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(this.dataSource);
    }
}
